package com.turing.post.cardmanagement.util;

import java.io.Serializable;

import com.turing.post.util.PCPSUtil;

/**
 * CPU卡根目录下的管理信息(EF05)
 * 
 * @author yxb
 * 
 */
@SuppressWarnings("serial")
public class CardIssueRoot implements Serializable {
	/**
	 * 01-01 版本信息 1 BCD
	 */
	private String version;

	/**
	 * 初始版本01
	 */
	public static final String VERSION_01 = "01";

	public void setVersion(String version) {
		this.version = version;
	}

	public String getVersion() {
		return version;
	}

	/**
	 * 02-04 发卡商号 3 BCD
	 */
	private String issueCompanyNo;

	public void setIssueCompanyNo(String issueCompanyNo) {
		this.issueCompanyNo = issueCompanyNo;
	}

	public String getIssueCompanyNo() {
		return issueCompanyNo;
	}

	/**
	 * 05-14 卡序号 9 BCD
	 */
	private String cardSeqNo;

	public void setCardSeqNo(String cardSeqNo) {
		this.cardSeqNo = cardSeqNo;
	}

	public String getCardSeqNo() {
		return cardSeqNo;
	}

	/**
	 * 将卡发行基本信息编码成16进制字符串以准备写卡
	 * 
	 * @return
	 */
	public String encodeIssueRoot() {
		StringBuilder sb = new StringBuilder();
		// 此处应检查数据的合法性
		sb.append(VERSION_01);
		sb.append(getIssueCompanyNo());
		sb.append(getCardSeqNo());
		return sb.toString();
	}

	/**
	 * 解析HEX字符串内容到卡发行基本信息对象
	 * 
	 * @param hexstr
	 * @return
	 */
	public static CardIssueRoot parseCardIssueRoot(String hexstr) {
		if (PCPSUtil.isEmpty(hexstr))
			return null;

		if (hexstr.length() < 26)
			return null;

		CardIssueRoot cir = new CardIssueRoot();

		// 解析version
		cir.setVersion(hexstr.substring(0, 2));

		// 解析issueCompanyNo
		cir.setIssueCompanyNo(hexstr.substring(2, 8));

		// 解析cardSeqNo
		cir.setCardSeqNo(hexstr.substring(8, 26));

		return cir;
	}
}
